package com.da.javatest.interview.other.cglib3;

import org.springframework.cglib.proxy.Dispatcher;
import org.springframework.cglib.proxy.Enhancer;

/**
 * @author chenlida
 * @date 2020/6/5 23:31
 * @description
 */
public class CglibDispatcher {
    public static class UserModel {
        private String name;

        public UserModel() {
        }

        public UserModel(String name) {
            this.name = name;
        }

        public void say() {
            System.out.println("你好：" + name);
        }
    }

    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(CglibLazyLoad.UserModel.class);
        enhancer.setCallback((Dispatcher)() -> {
            System.out.println("Dispatcher.loadObject()方法");
            return new CglibLazyLoad.UserModel("达少");
        });

        CglibLazyLoad.UserModel userModel = (CglibLazyLoad.UserModel)enhancer.create();
        System.out.println("第1次调用say方法");
        userModel.say();
        System.out.println("第2次调用say方法");
        userModel.say();
    }
}
